import java.util.HashMap;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 九转苍翎
 * Date: 2025/8/26
 * Time: 18:43
 */
public class Solution {
    public int subarraysDivByK(int[] nums, int k) {
        int sum = 0,ret = 0;
        HashMap<Integer, Integer> map = new HashMap<>();
        map.put(0, 1);
        for (int num : nums) {
            sum += num;//计算当前位置的前缀和
            int target = (sum % k + k) % k;
            ret += map.getOrDefault(target, 0);
            map.put(target,map.getOrDefault(target,0)+1);
        }
        return ret;
    }
    public static void main(String[] args) {
        Solution solution = new Solution();
        int ret = solution.subarraysDivByK(new int[]{1, 2, 3, 4, 5}, 3);
        System.out.println(ret);
    }
}